package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.operatern;
import cn.com.hfuu.module.operatesq;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.RnService;
import cn.com.hfuu.service.SqService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.vo.Page;


@RequestMapping("rn")
@Controller
public class RnControl {
 
	@Autowired
	RnService rnService;
	
	@Autowired
	OperateService operateService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	TeaxService teaxService;
	
	@RequestMapping("info")
	public String info(){
		return "operate/rn/rn";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, operatern rn){
		List<operatern>  list=rnService.selectPage(start,limit,rn);
		List<operatern> total=rnService.selectpagetotal(rn);
		for (operatern rn1 : list) {
			rn1.setUser(userService.selectuserbyid(rn1.getOperateUserid()));
			rn1.setWarehouseteax(teaxService.selectbyid(rn1.getOperateTeaxid()));
			rn1.toString();
		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
 		List<operatern> list =rnService.selectpagetotal(new operatern());
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (operatern rn1 : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			rn1.setUser(userService.selectuserbyid(rn1.getOperateUserid()));
			rn1.setWarehouseteax(teaxService.selectbyid(rn1.getOperateTeaxid()));
			 
			
			map1.put("编号", rn1.getOperateRnid());
			map1.put("茶名",rn1.getWarehouseteax().getWarehouseTeaname());
 
			map1.put("用时(min)", rn1.getOperateTotaltime());
	 
			map1.put("操作人", rn1.getUser().getUserName());
			map1.put("操作时间", rn1.getOperateTime());
			mapList.add(map1);

		}
		String[] title = { "编号", "茶名",  "用时(min)","操作人","操作时间" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶叶揉捻信息");
		return new ModelAndView(ve, map);
	}
	
	@RequestMapping("listad")
	@ResponseBody
	public List<operatern> listad(){

		List<operatern> total=rnService.selectpagetotal(new operatern());
		for (operatern rn1 : total) {
			rn1.setUser(userService.selectuserbyid(rn1.getOperateUserid()));
			rn1.setWarehouseteax(teaxService.selectbyid(rn1.getOperateTeaxid()));
			rn1.toString();
		}
 
		return total;
	}
	
	@RequestMapping("edit")
	public ModelAndView edit(String id){
		operatern rn=rnService.selectbyid(id);
		warehouseteax teax =teaxService.selectbyid(rn.getOperateTeaxid());
		ModelAndView mv=new ModelAndView();
		mv.addObject("rn", rn);
		mv.addObject("teax", teax);
		mv.setViewName("operate/rn/edit");
		return mv;
		
	}
	
	@RequestMapping("editproc")
	@ResponseBody
	public String editproc(operatern rn){
		if(rnService.update(rn)>0){
			return "success";
		}
		return null;
		
	}
	
	/**
	 * 删除杀青
	 * 1、未进行揉捻
	 * 2、删除杀青
	 * 3、更新操作表
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String id){
		operatern sq=rnService.selectbyid(id);
		operate op=operateService.selectbyid(sq.getOperateId());
		if(op.getWarehouseState()!=2){//进行干燥
			return "false";
		}else{
			op.setWarehouseState(1);
			operateService.update(op);
			rnService.deletebyid(id);
			return "success";
		}
		
	}
}
